package com.mb.ext.core.dao.impl.group;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.NoResultException;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.mb.ext.core.dao.AbstractBaseDAO;
import com.mb.ext.core.dao.group.GroupBuyDefDAO;
import com.mb.ext.core.entity.group.GroupBuyDefEntity;
import com.mb.ext.core.entity.merchant.MerchantEntity;
import com.mb.ext.core.service.spec.group.GroupBuyDefOrderDTO;
import com.mb.framework.exception.DAOException;
import com.mb.framework.util.log.LogHelper;

@Repository("groupBuyDefDAO")
@Qualifier("groupBuyDefDAO")
public class GroupBuyDefDAOImpl extends AbstractBaseDAO<GroupBuyDefEntity> implements GroupBuyDefDAO {
	private final LogHelper logger = LogHelper.getInstance(this.getClass().getName());

	/**
	 * Initializing Entity.
	 */
	public GroupBuyDefDAOImpl() {
		super();
		this.setEntityClass(GroupBuyDefEntity.class);
	}

	@Override
	public void addGroupBuy(GroupBuyDefEntity groupBuyDefEntity) throws DAOException {

		this.save(groupBuyDefEntity);

	}

	@Override
	public void updateGroupBuy(GroupBuyDefEntity groupBuyDefEntity) throws DAOException {

		this.update(groupBuyDefEntity);

	}

	@Override
	public void deleteGroupBuy(GroupBuyDefEntity groupBuyDefEntity) throws DAOException {

		this.delete(groupBuyDefEntity);

	}

	@Override
	public GroupBuyDefEntity getGroupBuyByUuid(String uuid) throws DAOException {
		GroupBuyDefEntity groupBuyDefEntity = null;
		try {
			groupBuyDefEntity = (GroupBuyDefEntity) em.createQuery(
					"select b from GroupBuyDefEntity b where  b.groupBuyDefUuid = :UUID and b.isDeleted=:isDeleted",
					GroupBuyDefEntity.class).setParameter("UUID", uuid).setParameter("isDeleted", Boolean.FALSE)
					.getSingleResult();
		} catch (NoResultException e) {
			logger.info("No record found for def : " + uuid);
		}
		return groupBuyDefEntity;
	}

	@Override
	public List<GroupBuyDefEntity> getActiveGroupBuys() throws DAOException {
		List<GroupBuyDefEntity> groupBuyDefEntityList = new ArrayList<GroupBuyDefEntity>();
		try {
			groupBuyDefEntityList = em.createQuery(
					"select b from GroupBuyDefEntity b where b.status = :STATUS and b.isDeleted=:isDeleted order by b.createDate desc",
					GroupBuyDefEntity.class)
					.setParameter("STATUS", "1")
					.setParameter("isDeleted", Boolean.FALSE).getResultList();
		} catch (NoResultException e) {
			logger.info("No record found for sec kill");
		}
		return groupBuyDefEntityList;
	}
	
	@Override
	public List<GroupBuyDefEntity> getGroupBuysByMerchant(MerchantEntity merchantEntity) throws DAOException {
		List<GroupBuyDefEntity> groupBuyDefEntityList = new ArrayList<GroupBuyDefEntity>();
		try {
			groupBuyDefEntityList = em.createQuery(
					"select b from GroupBuyDefEntity b where b.merchantEntity.merchantUuid = :MERCHANTUUID and b.isDeleted=:isDeleted order by b.createDate desc",
					GroupBuyDefEntity.class)
					.setParameter("MERCHANTUUID", merchantEntity.getMerchantUuid())
					.setParameter("isDeleted", Boolean.FALSE).getResultList();
		} catch (NoResultException e) {
			logger.info("No record found for sec kill");
		}
		return groupBuyDefEntityList;
	}

	@Override
	public List<GroupBuyDefEntity> getAllGroupBuys() throws DAOException {
		List<GroupBuyDefEntity> groupBuyDefEntityList = new ArrayList<GroupBuyDefEntity>();
		try {
			groupBuyDefEntityList = em.createQuery("select b from GroupBuyDefEntity b where  b.isDeleted=:isDeleted order by b.createDate desc",GroupBuyDefEntity.class).setParameter("isDeleted", Boolean.FALSE).getResultList();
		} catch (NoResultException e) {
			logger.info("No record found for sec kill");
		}
		return groupBuyDefEntityList;
	}

	@Override
	public List<GroupBuyDefOrderDTO> getGroupBuyOrders(String groupBuyDefUuid) throws DAOException {
		List<GroupBuyDefOrderDTO> dtoList = new ArrayList<GroupBuyDefOrderDTO>();
		try {
			List<Object> resultList = new ArrayList<Object>();
			String sqlStr = "select m.CONTACTNAME,m.MOBILENO,concat(m.MERCHANTADDRESS,m.MERCHANTNAME),op.PRODUCT_NAME,op.product_sku_desc,sum(op.PRODUCT_UNIT) from merchant as m, `order` as o ,order_product as op, group_buy_def as gbe, group_buy_product as gbp where gbe.GROUP_BUY_DEF_UUID = :GROUPBUYDEFUUID and  o.ORDER_STATUS not in ('0','4') and m.MERCHANT_UUID = o.MERCHANT_UUID and o.ORDER_UUID = op.ORDER_UUID and op.GROUP_BUY_PRODUCT_UUID = gbp.GROUP_BUY_PRODUCT_UUID and gbp.GROUP_BUY_DEF_UUID and gbe.GROUP_BUY_DEF_UUID group by op.PRODUCT_NAME,op.PRODUCT_SKU_DESC";
			resultList = em.createNativeQuery(sqlStr)
					.setParameter("GROUPBUYDEFUUID", groupBuyDefUuid)
					.getResultList();
			for (int i=0; i<resultList.size();i++){
				Object[] result = (Object[]) resultList.get(i);
				String merchantName = String.valueOf(result[0]);
				String mobileNo = String.valueOf(result[1]);
				String merchantAddress = String.valueOf(result[2]);
				String productName = String.valueOf(result[3]);
				String productSkuName = String.valueOf(result[4]);
				BigDecimal unit = (BigDecimal) result[5];
				GroupBuyDefOrderDTO dto = new GroupBuyDefOrderDTO();
				dto.setMerchantName(merchantName);
				dto.setMerchantAddress(merchantAddress);
				dto.setMobileNo(mobileNo);
				dto.setProductName(productName);
				dto.setProductSkuName(productSkuName);
				dto.setUnit(unit.intValue());
				dtoList.add(dto);
			}
		} catch (NoResultException e) {
			logger.info("No record found for merchant: ");
		}
		return dtoList;
	}
}
